package com.watchthelan.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class Validator {
	
	public static boolean validUsername(String username) {
		if ("".equals(username)) {
			return false;
		}
		if (username.contains("~!@#$%^&*(){}[]:;'|<,>/?") 
				|| username.indexOf('\\') >= 0 
				|| username.indexOf('\'') >=0) {
			return false;
		}
		return true;
	}
	
	public static boolean validPhone(String phone) {
		if ("".equals(phone)) {
			return false;
		}
		Pattern p = Pattern.compile("^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$");
		Matcher m = p.matcher(phone);
		if (!m.matches()) {
			return false;
		}
		return true;
	}
	
	public static boolean validEmail(String email) {
		if ("".equals(email)) {
			return false;
		}
		Pattern p = Pattern.compile("^[\\w\\.-]+@([\\w\\-]+\\.)+[a-z]{2,4}$");
		Matcher m = p.matcher(email);
		if (!m.matches()) {
			return false;
		}
		return true;
	}
	
	public static boolean validPort(String number) {
		if ("".equals(number)) {
			return false;
		}
		try {
			int port = Integer.parseInt(number);
		} catch (Exception e) {
			return false;
		}
		return true;
	}
	
	public static boolean validHostname(String hostname) {
		if ("".equals(hostname)) {
			return false;
		}
		if (hostname.indexOf(' ') >= 0) {
			return false;
		}
		return true;
	}
	
	public static boolean validIp(String ip) {
		if ("".equals(ip)) {
			return false;
		}
		String _255 = "(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)";
		Pattern p = Pattern.compile("^(?:" + _255 + "\\.){3}" + _255 + "$");
		Matcher m = p.matcher(ip);
		if (!m.matches()) {
			return false;
		}
		return true;
	}
}
